import {FairyGUI, FairyEditor, System} from 'csharp';

const App = FairyEditor.App;

App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/CustomInspector')

const CUR_PATH = eval("__dirname");
const Path_Project = CUR_PATH.substr( 0 , CUR_PATH.indexOf("/") );
const Path_Bin = Path_Project + "/bin/";
const ROOT_BIN = FairyEditor.App.pluginManager.basePath + "/" + Path_Bin;


function addMenuBtn(){
    let menu = FairyEditor.App.menu.GetSubMenu( "help" );
    menu && menu.AddSeperator();//添加分割
    menu && menu.AddItem( "测试插件界面" , "test" , (str)=>{
        console.log("### touch btn")
        this.updateLoder();
    } );
}

// 更新激活窗口的 加载器
function updateLoder(){
    let document:FairyEditor.FComponent = App.activeDoc.content;
    let length = document.children.Count;
    for (let i = 0; i < length; i++) {
        let item = document.children.get_Item(i);
        console.log(item.objectType)
    }
}


class DemoInspector extends FairyEditor.View.PluginInspector {
    private combo: FairyGUI.GComboBox;

    public constructor() {
        super();

        this.panel = FairyGUI.UIPackage.CreateObject("CustomInspector", "Component1").asCom;
        this.combo = this.panel.GetChild("browser").asComboBox;
        this.combo.onChanged.Add(() => {
            let obj = App.activeDoc.inspectingTarget
            //use obj.docElement.SetProperty('xxx',..) instead of obj.xxx = ... to enable undo/redo mechanism
            obj.docElement.SetProperty("customData", this.combo.value)
        });
        // App.project.
        this.updateAction = () => { return this.updateUI(); };
        // FairyEditor.FPackage.GetItem("")
        // var res =FairyGUI.UIPackage.CreateObject("UIBase","VIP_png");
        // var res = FairyGUI.UIPackage.GetItemURL("UIBase", "VIP_png");
        // console.log("###",res)
    }

    private updateUI(): boolean {
        let sels = App.activeDoc.inspectingTargets
        let obj = sels.get_Item(0);
        if(obj.objectType == "loader"){
            App.consoleView.Log("select target: "+obj.objectType+obj.name)
            let img = FairyEditor.VImage.New( "ui://pz5k4qhbjbta3i" );
            let texture = new FairyGUI.NTexture( img.ToTexture(true,true)  );
        }
        this.combo.value = obj.customData

        return true; //if everything is ok, return false to hide the inspector
    }
}

//Register a inspector
App.inspectorView.AddInspector(() => new DemoInspector(), "CustomInspectorJS", "CustomInspector");
//Condition to show it
App.docFactory.ConnectInspector("CustomInspectorJS", "mixed", false, false);

function onPublish(handler: FairyEditor.PublishHandler) {
    let pkg = handler.GetItemDesc(null) as FairyGUI.Utils.XML;
    let id = pkg.GetAttribute("id");
    let match = id.match(/\$\w+/g); // 根据标识符$进行窜改
    if (match) {
        let tag = match[0];
        id = id.replace(tag, "");
        pkg.SetAttribute("id", id);
        handler.ExportBinaryDesc(pkg.ToString());
    }
}

function onPublish3(handler) {
    console.log("### onPublish ", handler, handler.fileName);
    let pkg = handler.GetItemDesc(null)  as FairyGUI.Utils.XML;
    let myEnumerator:FairyGUI.Utils.XMLList.Enumerator = pkg.attributes.GetEnumerator() as FairyGUI.Utils.XMLList.Enumerator
    myEnumerator.Current.name
}

function onDestroy() {
    //do cleanup here
}